﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Data.Core;
using Spring.Data.Common;
using System.Data;
using Spring.Data;

namespace Smart.Data
{
    /// <summary>
    /// 数据模型对象
    /// </summary>
    public class DataHelper
    {
        public DataHelper(IDataSource source)
        {
            Source = source;
            Template = CreateAdoTemplate();
        }

        protected IDataSource Source = null;
        protected AdoTemplate Template = null;

        protected virtual AdoTemplate CreateAdoTemplate()
        {
            var provider = DbProviderFactory.GetDbProvider(Source.Provider);
            provider.ConnectionString = Source.ToConnectionString();
            return new AdoTemplate(provider, true);
        }

        /// <summary>
        /// 执行回调方法
        /// </summary>
        /// <param name="func"></param>
        /// <returns></returns>
        public object Execute(Func<IDbCommand, object> func)
        {
            return Template.Execute(new InnerExecuter() { Func = func });
        }

        /// <summary>
        /// 执行非查询语句
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public int ExecuteNonQuery(string cmd)
        {
            return Template.ExecuteNonQuery(System.Data.CommandType.Text, cmd);
        }

        /// <summary>
        /// 查询首行首列
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public object ExecuteScalar(string cmd)
        {
            return Template.ExecuteScalar(System.Data.CommandType.Text, cmd);
        }

        /// <summary>
        /// 查询数据表
        /// </summary>
        /// <param name="select"></param>
        /// <returns></returns>
        public DataTable Create(string select)
        {
            return Create(select, "DataTable");
        }

        /// <summary>
        /// 查询数据表
        /// </summary>
        /// <param name="select"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public DataTable Create(string select, string name)
        {
            var table = Template.DataTableCreate(CommandType.Text, select);
            table.TableName = name;
            return table;
        }

        /// <summary>
        /// 更新数据表
        /// </summary>
        /// <param name="source"></param>
        /// <param name="select"></param>
        public void Update(DataTable source, string select)
        {
            Template.DataTableUpdateWithCommandBuilder(source, CommandType.Text, select, null, source.TableName);
        }

        /// <summary>
        /// 内部回调执行类
        /// </summary>
        class InnerExecuter : Spring.Data.ICommandCallback
        {
            /// <summary>
            /// 回调方法
            /// </summary>
            public Func<IDbCommand, object> Func { get; set; }

            public object DoInCommand(IDbCommand command)
            {
                return Func(command);
            }
        }
    }
}
